package com.sptpc.learnspring.controller;

import com.sptpc.learnspring.controller.request.NewOrderRequest;
import com.sptpc.learnspring.controller.request.OrderStateRequest;
import com.sptpc.learnspring.model.Order;
import com.sptpc.learnspring.service.IOrderService;
import com.sptpc.learnspring.vo.OrderVO;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private IOrderService orderService;

    @PostMapping("/add")
    public Order createNewOrder(@Valid @RequestBody NewOrderRequest orderRequest) {
        log.info("收到新订单:{}", orderRequest);
        Order order = orderService.addNewOrder(orderRequest);
        return order;
    }

    @PutMapping("/{id}")
    public Order updateState(@PathVariable("id") Long id,
                             @RequestBody OrderStateRequest orderState) {
        log.info("更新订单状态:{}", orderState);
        Order order = orderService.getOrder(id);
        orderService.updateState(order, orderState.getState());
        return order;
    }

    @GetMapping("/{id}")
    public OrderVO getOrderWithDrinksById(@PathVariable("id")Long id) {
        return orderService.getOrderWithDrinksById(id);
    }
}
